var Weapon = require("Weapon");
var EnemyDamager = require("EnemyDamager");
var UIController = require("UIController");
var SFXManager = require("SFXManager");

cc.Class({
    extends: Weapon,

    properties: {
        rotateSpeed: 1,

        degree : {
			default: 0,
			type: cc.Integer,
			visible: false,
		},
        radians : {
			default: 0,
			type: cc.Float,
			visible: false,
		},

        star: cc.Prefab,
		starHolder: cc.Node,

        liveTime: 0,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        //星星存在周期等于此武器使用周期,目前用于测试
        this.liveTime = this.stats[this.weaponLevel].duration;
    },

    start() {
        this.spawnStars();

    },

    update(dt) {
        this.starHolder.angle -= this.rotateSpeed * dt * this.stats[this.weaponLevel].speed;

        if (this.statsUpdated == true) {
            this.statsUpdated = false;
            this.unscheduleAllCallbacks();
            this.spawnStarsOnce();
            this.spawnStars();
        }
    },

    spawnStars() {
        this.schedule(() => {
            this.spawnStarsOnce();
            
        }, this.stats[this.weaponLevel].duration);
    },

    spawnStarsOnce() {
        let perDrgree = 360 / this.stats[this.weaponLevel].amount;
        let preRadians = cc.misc.degreesToRadians(perDrgree);
        for (let index = 0; index < this.stats[this.weaponLevel].amount; index++) {

            let starBullet = cc.instantiate(this.star);
            starBullet.angle = -(perDrgree * index);

            starBullet.parent = this.starHolder;


            starBullet.setPosition(cc.v2(this.position).add(cc.v2(this.stats[this.weaponLevel].range * Math.sin(preRadians * index), this.stats[this.weaponLevel].range * Math.cos(preRadians * index))));

            this.setStats(starBullet);

            let starShot = cc.tween(starBullet)
                .to(0.4, { scale: 1, opacity: 255 })
                .delay(this.liveTime - 1)
                .to(0.4, { scale: 0.1, opacity: 55 })
                .removeSelf()
                .start();
            }
    },

    setStats (starBullet) {
        starBullet.getComponent('EnemyDamager').isBullet = true;
        starBullet.getComponent('EnemyDamager').validHits = -1;
		starBullet.getComponent('EnemyDamager').damageAmount = this.stats[this.weaponLevel].damage;

    },
});
